﻿using Dapper;
using Finance.Api.Read.Applications.Commands.财务管理;
using Finance.Api.Read.Dtos;
using Finance.ErrorCode;
using Finance.Infrastructure;
using MediatR;
using System.Security.Cryptography.Xml;
using System.Text;

namespace Finance.Api.Read.Applications.CommandHands.财务管理
{
    public class RefundQueryCommandHandlers : IRequestHandler<RefundQueryCommand, ApiResult< ApiPaging<List<RefundDto>> >>
    {
        private readonly IDapperDb _db;

        public RefundQueryCommandHandlers(IDapperDb db)
        {
            _db = db;
        }

        public async Task<ApiResult<ApiPaging<List<RefundDto>>>> Handle(RefundQueryCommand request, CancellationToken cancellationToken)
        {
            var result=new ApiResult<ApiPaging<List<RefundDto>>>();
            StringBuilder stringBuilders=new StringBuilder();
            StringBuilder Totalnum=new StringBuilder();
            ApiPaging<List<RefundDto>> res=new ApiPaging<List<RefundDto>>();
            var parmenters = new DynamicParameters();
            try
            {
                stringBuilders.Append(@"SELECT a.SupplierId,
                                        a.PaymentAmount,
                                        a.PaymentAccount,
                                        a.PaymentDate,
                                        a.Operator,
                                        a.Summary,
                                        b.SuppliersName AS SuppliersName,
                                        a.CreateName,
                                        a.CreateTime,
                                        a.UpdateName,
                                        a.UpdateTime,
                                        a.DeleteName,
                                        a.DeleteTime
                                FROM FinanceRefund a
                                JOIN SuppliersModel b
                                     ON a.SupplierId=b.Id  where 1=1 and a.IsDelete=0");
                if (!string.IsNullOrEmpty(request.SupplierName))
                {
                    stringBuilders.Append(" and b.SuppliersName LIKE @SuppliersName ");
                    parmenters.Add("SuppliersName", $"%{request.SupplierName}%");
                
                }
                if (!string.IsNullOrEmpty(request.PaymentAccount))
                {
                    stringBuilders.Append("and a.PaymentAccount = @PaymentAccount");
                    parmenters.Add("PaymentAccount", $"{request.PaymentAccount}");
                }
                Totalnum.Append(@"SELECT COUNT(1)
                            FROM FinanceRefund ");
                if (!string.IsNullOrEmpty(request.SupplierName))
                {
                    stringBuilders.Append(" and b.SuppliersName LIKE @SuppliersName ");
                  

                }
                if (!string.IsNullOrEmpty(request.PaymentAccount))
                {
                    stringBuilders.Append("and a.PaymentAccount = @PaymentAccount");
                   
                }
                int totalcount = await _db.QueryFirst<int>(Totalnum.ToString(), parmenters);
                stringBuilders.Append($"order by a.Id DESC offset ({request.index - 1})*{request.size} rows fetch next {request.size} rows only");


                var list = await _db.Query<RefundDto>(stringBuilders.ToString(), parmenters);
                res.data=list.ToList();
                res.totalcount = totalcount;
                res.totalpcount = (int)Math.Ceiling(totalcount * 1.0 / request.size);
                result.data = res;
                result.message = "分页查询成功";
                result.Code = Enumcode.ok;
            }
            catch (Exception)
            {


                result.message = "分页查询失败";
                result.Code = Enumcode.fail;
                
            }




            return await Task.FromResult(result);
        }
    }
}
